1. arrays

在之前的通话中,我们研究了 Coats 中提供的许多不同的数据变量,从贵重金条到时间变量、长卷轴以及下一组视频中的许多其他变量。
In the previous calls, we looked at many different data variables that are available in Coats's, starting from the bullion valuables up to the time variables, long reels and many others in the next set of videos.

因此,我们将研究在本课程中将深入使用的更高级的数据类型。
So we will look into more advanced data types that we will use deeply during this course.

所以它们非常有用,您将在接下来的视频中了解原因。
So they are very useful and you will see why in the next videos.

这些类型在动机和结构上都是数组。
And these types are arrays in motives and structures.

因此,在本视频中,我们将讨论区域。
So in this video, we'll talk about areason.

我们在第一次粗化中短暂地看到了上涨。
We have briefly seen a raise in the first coarsen.

因此,让我们打开 Coats 并开始创建一个新的标准项目。
So let's open Coats's and start up by creating a new standard project.

现在,我们可以称其为无标题。
Right now, we can call it untitled.

我们并没有真正研究这种语言和结构化文本语言。
We are not really looking into this and a structured text language.

那么我们来定义一下什么是数组,数组只是具有指定维度的相同类型的数据的集合。
So let's define what an array is, an array is simply a collection of data of the same type with a specified dimension.

这是什么意思?
What does this mean?

好的,举例来说,我们有一个具有一定数量按钮的应用程序。
OK, let's say over here that we are, for instance, having an application with a certain number of buttons.

因此,我们将有许多不同的按钮以及连接到这些按钮的许多不同的变量。
So we will have many different buttons and many different variables connected to those buttons.

因此,例如,我们可以有一个简单的按钮,按下该变量后,该变量将指示我们是否按下了第一个按钮。
So, for instance, we could have a simply a button one pressed the variable that will indicate us if the first button is being pressed.

我们还需要两个、三个、四个等等。
We will also need that two, three, four and so on.

从按钮到按钮三个,假设我们有十个按钮。
Having button to button three, let's say we have ten buttons.

您想要做的是定义 10 个变量,即 10 个 BOLAM 变量。
What you would think to do is to define ten variables that 10 BOLAM variables.

这就是这可能行得通。
And this is is this may work.

这是可以做到的。
This can be done.

但实际上有一种更优雅的方法可以做到这一点。
But there is actually a more elegant way of doing this.

这个,呃,这个策略是使用数组。
And this, uh, this strategy is using arrays.

数组只是集合,是具有指定大小的相同类型的变量的集合。
Arrays are simply sets are simply collections of variables that are of the same type with the specified size.

因此,例如,我们可以有一个数组,而不是说它们有十个具有不同名称的底部类型的变量。
So, for instance, instead of saying they have ten variables that are that have different names of bottom types, we could have an array.

所以我们可以将一组布尔变量称为按钮,按下按钮。
So a set of boolean variables that we could call buttons, press the press.

这可以是布尔变量数组。
And this could be an array of boolean variables.

这就是我们找到两者的方法,并且我们可以在课程中找到数组。
This is how we can find both and we can find arrays in courses.

因此,首先您需要编写数组,然后需要在此处和此处打开方括号、打开和关闭方括号。
So first you need to write array, then you need to open square, open and close square brackets over here and here.

您需要指定数组的维度,这意味着数组的第一个索引是什么,最后一个索引是什么?
You need to specify the dimension of the array, meaning what is the first index of the array and what is the last one?

因此,如果我们说我们有 10、嗯、100 亿个变量,我们可以说我们的错误率范围为 1 到 10。
So if we are saying that we have ten, um, 10 billion variables, we could say that our error rate ranges from one to 10.

所以我将使用这种语法,即 10 和这种方式,并指定第一个第一个数据,数组的第一项是索引等于 1。
So I will use this syntax, one that that 10 and this way and specifying that the first the first data, the first item of the array is that the index is equal to one.

最后一个是指数等于10。
The last one is that the index equals ten.

然后我需要指定变量的类型。
Then I need to specify the type of variable.

所以我会写两者的数组。
So I will write the array of both.

这样我们就构建了一个包含十个体积变量的数组。
And this way we have built an array with ten volume variables.

如果我现在,如果我们这里没有程序,我们这里仍然没有编写代码。
If I right now, if we have no program over here, we still have no code written here.

我可以在模拟模式下上网。
I can go online in a simulation mode.

正如你在这里看到的,我们拥有的变量只是一个布尔变量数组,顺便说一句,加上这里我实际上可以看到所有 10 个变量和值。
And as you can see here, the variables that we have, it is simply one array of boolean variables, by the way, plus plus over here I can actually see all 10 variables and the values.

所以它们都是相互独立的。
So they are all independent from each other.

彼此之间唯一的联系是它们被收集到同一个数据集合、同一个数组中。
The only connection between each other is that they are collected into the same collection of data, into the same array.

但除此之外,他们是完全独立的。
But other than that, they are completely independent.

除了内存之外还分配了它们。
They are allocated in addition to memory.

所以奥尔德伯里是行不通的。
So Oldbury unviable.

如果我指向一个布尔变量,我会将另一个布尔变量放在远处的存储单元中。
If I point to one boolean variables, I will have the other one sitting in a distant memory cell.

因此,让我们尝试在这里编写一些代码来尝试与之交互。
So let's try to write some some code over here just to try to interact with this.

假设我按第一个,哦,首先,我们需要看看如何寻址,嗯,布尔变量,数组的单元格。
So let's say that if I press the first one oh, first of all, we need to see how to address the the, um, the boolean variables, the cells of the array.

因此,如果我们想访问第一个单元格,我们需要打开右侧按钮,按左方括号,数字一并关闭括号,这是数组的第一个单元格。
So if we want to access the first one and we need to open the right buttons, press the open square brackets, digit one and close the brackets, this is the first cell of the array.

您需要非常非常小心地寻址数组的单元格,因为如果您寻址不在数组中的单元格,您可能会做一些非常危险的事情,因为您可能正在访问一个内存区域,该内存区域是未分配给数组。
You need to be very, very careful into addressing the cells of the array, because if you address a cell that is not in the array, you may go you may do something that is really dangerous because you may be accessing a memory area that is not allocated for the array.

所以你可能会去写一些其他变量,这可能非常危险。
So you may go and write some other variables and that can be quite dangerous.

通常,宣泄中有一种方法可以防止这种情况发生。
Typically, there is a way in catharsis to prevent this.

我们将在另一个视频中更深入地探讨这一点。
We will look at this in a more deeply in another video.

但你通常应该尽量避免它。
But you typically you should try to avoid it.

您应该尽量避免添加避免在不在数组范围内的索引上的错误索引上写入数组。
You should try to avoid adding avoid writing an array on the wrong index on an index that is not in the array range.

所以我就在这里。
So I'm right over here.

该按钮按下了索引。
That button pressed the index.

抱歉,一应等于索引十处的一。
One shall be equal to the one at index ten sorry.

这样按下按钮,如果我改变第十个存储单元的值,没有人会相应改变。
Buttons pressed in this way, if I change the value of the tenth memory cell, no one will change accordingly.

那么让我们上网并进行下载。
So let's go online and carry out the download.

让我们开始程序吧。
Let's start the program.

如果我在这里更改最后一个值,您也会看到第一个值也发生变化。
And if I go over here and change the value of the last one, you will see the first one changing as well.

部分控制七结束。
Portion control end of seven.

所以都是独立的。
So there are all independent.

它们以您在代码中编写的方式进行交互。
They interact in the way that you write in the code.

在写任何东西之前,他们之间并不存在上述关系。
There is not a aforesaid relationship between them before writing anything.

因此,举例来说,现在,假设我想将这个按钮连接到某个计数器。
So, for instance, right now, let's say that I wanted to connect this this buttons to some counter.

所以让我们尝试在这里创建一个计数器。
So let's try to create a counter over here.

假设当按下按钮时,相应的计数器将增加 1。
Let's say that when a button is being pressed, a corresponding counter will increase of one in it.

所以在这里,我可以添加一个整数数组。
So over here, I may add, let's say an array of of integers.

因此,为了跟踪这个数字,我们将其称为计数器。
So in order to keep track of this number so let's call this counters.

这应该是,呃,这也应该是一个范围从一到十个无符号整数的数组。
This should be as well as, uh, this should be as well, an array that ranges from one to ten of, let's say, unsigned integers.

然后我们也连接这种行为,所以我们说,如果我们检测到变量的上升,我们想要增加计数器的值。
Then let's also connect this kind, this behavior, so we said that we wanted to increase the value of the counter if we detect a rising prigg of the variable.

因此,我们还创建一个由三个功能块组成的数组,您可能还记得,它们是用于检测布尔变量上升沿的块。
So let's also create an array of our three function blocks that, as you may remember, they are blocks that are being used to detect rising edges of boolean variables.

您还可以创建任何类型变量的数组,因此您也可以创建功能块的数组。
You can also create you can create arrays of any type of variables, so you can also create arrays of function blocks.

所以我们称这种上升触发为触发。
So let's call this rising trigger triggers.

这应该是包含 1 到 10 个三角函数类型变量的数组。
And this should be arrays ranging from one to 10 of our trig types of variables.

所以在这里,我们拥有实际执行任务所需的所有东西。
So over here, we have all all the things that we need to actually carry out the tasks.

那么我们现在该怎么办呢?
So what shall we do right now?

首先,我们应该首先为索引等于 1 的第一个调用 let's start this 。
First of all, we should start by calling let's start this for the first one for index equal to one.

首先,我们应该调用上升触发器。
First, we should call the rising trigger.

所以如果你还记得的话,你可以按F键转到这里。
So if you remember, you can press F to go over here.

你可以进入德克萨斯州开始写《崛起的扳机》。
You can go into the Texas start writing Rising Trigger.

正如您在这里所看到的,您看到了变量数组。
As you can see here, you see the array of the variables.

然后在这里它也指向一个未指定的单元格。
And then over here it is also pointing to an unspecified cell.

正如您所看到的,数据类型只是一个技巧,而不是我们到目前为止的技巧。
And as you can see, the data type is simply a trick, not our rate of our trick so far.

OK,我简单的有函数书的调用。
OK, I simply have the call of the function book.

那么让我们从第一个开始。
So let's start by the first one.

我现在要说的是,时钟变量、输入变量是按下的按钮。
And I shall say right now that the clock variables, the input variables is the buttons press the one.

这样,如果作为上升触发的一个按钮按下变成通过该提示的一个上升触发,则提示输出将变为通过。
So in this way, if buttons press of one as a rising trigger becomes through the rising trigger of one that cue, the cue output will become through.

只是对于单个采样时间,所以我在这里可以做的是,如果一个怀疑队列的上升触发器,那么如果这个变量为真,那么我只是简单地增加到相应计数器的值。
Just for a single sampling time, so what I can do over here is say that if rising triggers of one doubt queue, then if this variable is true, then I just simply increment to the value of the corresponding counter.

我将写其中一个的计数器。
I will write the counters of one.

应等于计数器一加一。
Shall be equal to counters of one plus one.

让我们看看这是否适用于第一个,让我们下载。
Let's see if this works for the first one, let's download.

如果我们进入按钮按下状态,我们就启动程序。
And let's start the program if we go into Button's pressed.

好吧,让我们展开一切,展开按钮,按下阵列,计数器就准备好了。
All right, let's expand everything, expand the buttons, press the array and the counters are ready.

如果我更改第一个控件的值,并且有七个,如您所见,相应的计数器会增加一个单位。
If I change the value of the first one control and have seven, as you can see, the corresponding counters as increased by one unit.

如果我说它分为三,那么,正如你所看到的,它正在计数。
If I said it falls into three, again, as you can see, it is counting.

现在让我们离线,我们将对所有其他国家/地区做同样的事情,因此最简单的方法应该是复制此代码,将其放在这里并用工具、咀嚼器替换所有代码。
Now let's go offline and we shall do the same thing for all the other countries, so the naive way to do it should be to copy this code, place it over here and replace all the ones with tools, chewers.

这有效。
This works.

你会写很多代码。
You will write a lot of code.

这将是非常非常长而且烦人的。
It will be very, very long and annoying.

因此,如果您有计算机科学背景,如果您对其他系统进行了编程,您可能知道我要讲的是什么以及为什么我提出数组特别有用的原因之一是您实际上可以索引,您实际上可以索引数组的大小,然后动态地索引。
So if you ha if you have a background in computer science, if you are programmed the other system, you may know where I'm going with this and why I raise is one reason why arrays are particularly useful is that you can actually index, you can actually index the size of the array and then dynamically.

所以我可以说的是我可以声明一个可以称为索引的变量,这应该是一个无符号整数变量。
So what I can say is I can declare a variable that I can call index and this shall be an unsigned integer variable.

我可以说我不想查看第一个单元格,但我想查看与索引变量对应的单元格,以便我可以看到上升的触发索引。
And I could say that I do not want to look at the first cell, but I want to look at the cell that correspond to the index variable so I can see rising trigger index.

我可以复制这个并将其放置在这里、这里以及任何地方,至少是之前的那个。
And I can copy this and placed this place it over here, over here and everywhere, where at least the one before.

这样,根据索引器的值,我将查看这些数组的一个单元格或另一个单元格。
So in this way, according to the value of indexer, I will look at one cell of these arrays or at another.

那么我在这里能做的就是使用 for 循环来遍历所有这些数组。
So what can I what I can do over here is use a for loop to go through all these arrays.

那么它对于 for 循环的作用是什么呢?它关心运行、一部分代码、一定数量的时间,同时增加或减少某个可雇佣的时间。
So what does it do for the for loops care a run, a portion of code, a certain number of time while increasing or decreasing a certain hireable.

因此,结构化文本中 for 循环的语法如下。
So the syntax for for loops in the structured text is the following.

因此对于等于一到十的索引。
So for index that is equal to one to ten.

这样,索引将从 1 开始,然后达到 10,并且在 for 循环结束时到期,需要使用制表符在行上编写和减少缩进。
In this way, index will start at one and will get to ten there and it is due at the end of the for loop and need to write and for less indent on the lines using tab.

现在我们拥有的是这四行代码。
And right now what we have here is that this four lines of code.

因此,我们将运行等于 1 到 2 到 3 的索引,最多为 10。
So we will run for index equal to one to two to three, up to ten.

我们已经成功地用很少的代码行以非常紧凑的方式做到了这一点。
And we have managed to do this in a really compact way with very few lines of code.

那么让我们上网下载吧。
So let's go online with the download.

让我们尝试一下这段代码,让我们打开变量、声明,然后尝试简单地更改某些 Bulimba 的值,并看到相应的计数器增加,正如您所看到的,它们确实增加了。
And let's try this code, so let's open our variables, declarations, and let's try to simply change the value of some Bulimba and see the corresponding counters increase, as you can see, they did.

让我们继续随机地做这个,然后看看。
Let's keep doing this randomly and see.

你可以看到它确实有效。
You can see that it actually works.

所以。
So.

因此,我们已经成功地使用数组和完整循环,以非常紧凑的方式为很多很多对象实际运行了这段代码。
So we have managed to actually run this code for many, many objects in a very compact way, using arrays and using full loops.

这很快就会实现,并且在课程的后面部分会派上用场。
This will come very soon and it will come in handy later on in the course.

而且这是一个非常强大的工具,在传统编程中被广泛使用。
And this is a very powerful tool that is in traditional programming is widely used.

当你有一组相同类型的对象、变量、功能时,你应该实际练习并开始使用它。
And you should actually practice this and start using this whenever you have a set of objects, of variables, of functionalities of the same type.